﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExpressionExtensions.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the Expressions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Reflection
{
    using System;
    using System.Linq.Expressions;

    public static class ExpressionExtensions
    {
        public static string ExtractMemberName<TMember>(this Expression<Func<TMember>> expression)
        {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression == null)
            {
                throw new ArgumentException(@"Expression is not a MemberExpression", "expression");
            }

            return memberExpression.Member.Name;
        }

        public static string ExtractMemberName<TType, TMember>(this Expression<Func<TType, TMember>> expression)
        {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression == null)
            {
                throw new ArgumentException("Expression is not a MemberExpression", "expression");
            }

            if (!typeof(TType).IsAssignableFrom(memberExpression.Expression.Type))
            {
                throw new ArgumentException("Expression is not a MemberExpression of the specified type.", "expression");
            }

            return memberExpression.Member.Name;
        }
    }
}
